// Package gen
// @Author: zhangdi
// @File: gen_one_api
// @Version: 1.0.0
// @Date: 2023/10/19 14:07
package gen

import (
	"fmt"
	"path/filepath"
	"server/pkg/filehelper"
)

// GenCodeAddApi 手动新增一个api，entityName和apiName传入大驼峰命名
func GenCodeAddApi(entityName, apiName string) {
	lineNameEntity := NameToLine(entityName) //下划线命名
	//cameSmalNameEntity := NameToCameSmall(entityName) //实体小驼峰命名法
	lineNameApi := NameToLine(apiName) //下划线命名(api名)
	controllerFuncName := fmt.Sprintf("%s%s", entityName, apiName)
	//向apis/xxx.go中追加
	goConstLine := fmt.Sprintf("const Api%s = \"/api/%s/%s\"\n", controllerFuncName, lineNameEntity, lineNameApi)
	err := AppendContentToFile(filepath.Join(GetGoDirConst(), "apis", lineNameEntity+".go"), goConstLine)
	if err != nil {
		println(err.Error())
	}
	//向controller文件追加内容
	goControllerContent := fmt.Sprintf("\nfunc %s(ctx *ginp.ContextPro) {\nvar params *requestdto.%s\n\tif err := ctx.ShouldBindJSON(&params); err != nil {\n\t\tctx.ErrJson(\"请求参数有误\" + err.Error())\n\t\treturn\n\t}}\n", controllerFuncName, controllerFuncName)
	controllerPath := filepath.Join(ServerDir(), "application", "controller", "custctrl", lineNameEntity+".go")
	err = AppendContentToFile(controllerPath, goControllerContent)
	if err != nil {
		println(err.Error())
	}

	//向router_xxx.go 中追加文件
	routerPath := filepath.Join(ServerDir(), "application", "router", "router_"+lineNameEntity+".go")
	replaceRouterContent := fmt.Sprintf("\n\tAppendRouter(ItemInfo{\n\t\tPath:           apis.Api%s,               //api路径\n\t\tHandlers:       ginp.ConvHandler(custctrl.%s), //对应控制器\n\t\tHttpType:       HttpPost,                                     //http请求类型\n\t\tNeedLogin:      false,                                         //是否需要登录\n\t\tNeedPermission: false,                                         //是否需要鉴权\n\t\tPermissionName: \"\" ,           //完整的权限名称,会跟权限表匹配\n\t\tSwagger: &SwaggerInfo{\n\t\t\tTitle:       \"%s\",\n\t\t\tRequestDto:  requestdto.%s{},\n\t\t\tDescription: \"\",\n\t\t},\n\t})\n\t//{{append}}\n",
		controllerFuncName, controllerFuncName, controllerFuncName, controllerFuncName)
	err = replaceTextInFile(routerPath, "//{{append}}", replaceRouterContent)
	if err != nil {
		return
	}

	//构造dto文件--go文件
	dtoDir := filepath.Join(GetGoDirApplication(), "dto", "requestdto")
	filehelper.CreateDir(dtoDir)
	goDtoFilepath := filepath.Join(dtoDir, lineNameEntity+".go")
	if !filehelper.FileExists(goDtoFilepath) {
		filehelper.WriteContent(goDtoFilepath, "package requestdto\n")
	}
	goDtoContent := fmt.Sprintf("\ntype %s struct {}", controllerFuncName)
	err = AppendContentToFile(goDtoFilepath, goDtoContent)
	if err != nil {
		println(err.Error())
	}

	//构造前端常量 apis/xxx.ts
	tsConstLine := fmt.Sprintf("\nexport const Api%s%s = \"/api/%s/%s\"\n", entityName, apiName, lineNameEntity, lineNameApi)
	tsConstPath := filepath.Join(GetTsDirConstApis(), "apis", lineNameEntity+".ts")
	err = AppendContentToFile(tsConstPath, tsConstLine)
	if err != nil {
		println(err.Error())
	}

	//构造前端请求方法 api/modules/xxx.ts
	//moduleFilePath := filepath.Join(FrontDir(), "src", "api", "modules", cameSmalNameEntity+".ts")
	//tsModuleContent := fmt.Sprintf("export const request%s = (params: any) => {\n  return http.post(Api%s, params);\n};", controllerFuncName, controllerFuncName)
	//err = AppendContentToFile(moduleFilePath, tsModuleContent)
	//if err != nil {
	//	println(err.Error())
	//}

}
